//
//  YYJ_MBHud.m
//  Co8
//
//  Created by Hanzengchao on 2017/12/6.
//

#import "YYJ_MBHud.h"
#import "UIImage+GIF.h"
#import "ANDA_MonggoLabel.h"
@interface YYJ_MBHud()<MBProgressHUDDelegate>{
   
}
@end

@implementation YYJ_MBHud

+(instancetype)shareinstance{
    static YYJ_MBHud *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[YYJ_MBHud alloc] init];
    });
    return instance;
}
+(void)show:(NSString *)msg inView:(UIView *)view mode:(Co8MBProgressMode)myMode{
    //如果已有弹框，先消失
    if ([YYJ_MBHud shareinstance].hud != nil) {
        [[YYJ_MBHud shareinstance].hud hideAnimated:YES];
        [YYJ_MBHud shareinstance].hud = nil;
    }
    //4\4s屏幕避免键盘存在时遮挡
    if ([UIScreen mainScreen].bounds.size.height == 480) {
        [view endEditing:YES];
    }
    [YYJ_MBHud shareinstance].hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    [YYJ_MBHud shareinstance].hud.alpha = 1;    //是否显示透明背景
    [YYJ_MBHud shareinstance].hud.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.6];
    [[YYJ_MBHud shareinstance].hud setMargin:10];
    [[YYJ_MBHud shareinstance].hud setRemoveFromSuperViewOnHide:YES];
    
    [YYJ_MBHud shareinstance].hud.detailsLabel.text = [msg isKindOfClass:[NSString class]]? msg : @"";
    [YYJ_MBHud shareinstance].hud.contentColor = [UIColor whiteColor];
    [YYJ_MBHud shareinstance].hud.detailsLabel.font = [UIFont fontWithName:@"MongolianWhite" size:15];
    if (myMode == Co8MBProgressModeOnlyText) {
        if (!language_is_mon) {
            myMode = Co8MBProgressModeOnlyText_zh;
        }
    }
    switch ((NSInteger)myMode) {
        case Co8MBProgressModeOnlyText:{
//            [YYJ_MBHud shareinstance].hud.mode = MBProgressHUDModeText;
            [YYJ_MBHud shareinstance].hud.detailsLabel.text = @"";
            [YYJ_MBHud shareinstance].hud.mode = MBProgressHUDModeCustomView;
            ANDA_MonggoLabel *text = [[ANDA_MonggoLabel alloc] init];
            text.backgroundColor = [UIColor clearColor];
            text.kTitleColor = [UIColor whiteColor];
            text.kNewFontSize = 15;
            text.kTitle = [msg isKindOfClass:[NSString class]]? msg : @"";
            CGFloat height = YYJScreenHeight*0.6;
            CGSize labelSize = [text getSizeWidth:height andHeight:MAXFLOAT andString:@""];
            CGFloat width = labelSize.height+1;
            height = labelSize.width+1;
            text.frame = CGRectMake(0, 0, width, height);
            [YYJ_MBHud shareinstance].hud.customView = text;
        }
            break;
        case Co8MBProgressModeOnlyText_zh:{
            [YYJ_MBHud shareinstance].hud.mode = MBProgressHUDModeText;
            [YYJ_MBHud shareinstance].hud.detailsLabel.text = msg;
        }
            break;
            
        case Co8MBProgressModeLoading:
            [YYJ_MBHud shareinstance].hud.mode = MBProgressHUDModeIndeterminate;
            break;
            
        case Co8MBProgressModeCircleLoading:
            [YYJ_MBHud shareinstance].hud.mode = MBProgressHUDModeDeterminate;
            break;
            
        case Co8MBProgressModeSuccess:
            [YYJ_MBHud shareinstance].hud.mode = MBProgressHUDModeCustomView;
            [YYJ_MBHud shareinstance].hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"hzc_Selected_Box_Tick"]];
            break;
            
        default:
            break;
    }
}
+(void)showView:(UIView *)view{
    [self show:@"" inView:view mode:Co8MBProgressModeLoading];
}
+(void)hide{
    if ([YYJ_MBHud shareinstance].hud != nil) {
        [[YYJ_MBHud shareinstance].hud hideAnimated:YES];
    }
}
+(void)showLodingMessage:(NSString *)msg inView:(UIView *)view{
    [self show:msg inView:view mode:Co8MBProgressModeLoading];
}
+(void)showMessage:(NSString *)msg inView:(UIView *)view{
    
    if (msg.length>0) {
        if (!language_is_mon) {
            [self show:msg inView:view mode:Co8MBProgressModeOnlyText_zh];
        }else{
            [self show:msg inView:view mode:Co8MBProgressModeOnlyText];
        }
    }else{
        [self show:msg inView:view mode:Co8MBProgressModeOnlyText];
    }
    [[YYJ_MBHud shareinstance].hud hideAnimated:YES afterDelay:1.0];
}
+ (BOOL)isChineseWithStr:(NSString *)str
{
    for(int i=0; i< [str length];i ++)
    {
        int a = [str characterAtIndex:i];
        
        if( a > 0x4e00 && a < 0x9fff)
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }
    return NO;
}
+(void)showMessage_zh:(NSString *)msg inView:(UIView *)view{
    [self show:msg inView:view mode:Co8MBProgressModeOnlyText_zh];
    [[YYJ_MBHud shareinstance].hud hideAnimated:YES afterDelay:1.0];
}
+(void)showOnlyText:(NSString *)msg inView:(UIView *)view{
    [self show:msg inView:view mode:Co8MBProgressModeOnlyText];
}
+(void)showMessage:(NSString *)msg inView:(UIView *)view afterDelayTime:(NSInteger)delay{
    [self show:msg inView:view mode:Co8MBProgressModeOnlyText];
    [[YYJ_MBHud shareinstance].hud hideAnimated:YES afterDelay:delay];
}

+(void)showSuccess:(NSString *)msg inview:(UIView *)view{
    [self show:msg inView:view mode:Co8MBProgressModeSuccess];
    [[YYJ_MBHud shareinstance].hud hideAnimated:YES afterDelay:1.0];
}
+(void)showProgress:(NSString *)msg inView:(UIView *)view{
    [self show:msg inView:view mode:Co8MBProgressModeLoading];
}
+(void)showMsgWithoutView:(NSString *)msg{
    UIWindow *view = [[UIApplication sharedApplication].windows lastObject];
    [self show:msg inView:view mode:Co8MBProgressModeOnlyText];
    [[YYJ_MBHud shareinstance].hud hideAnimated:YES afterDelay:1.0];
}
+(void)showProgress:(CGFloat)progress title:(NSString *)msg inView:(UIView *)view{
    //如果已有弹框，先消失
    if ([YYJ_MBHud shareinstance].hud ){
        [YYJ_MBHud shareinstance].hud.progress = progress;
        return;
    }
    //4\4s屏幕避免键盘存在时遮挡
    if ([UIScreen mainScreen].bounds.size.height == 480) {
        [view endEditing:YES];
    }
    [YYJ_MBHud shareinstance].hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    [YYJ_MBHud shareinstance].hud.alpha = 1;    //是否显示透明背景
    [YYJ_MBHud shareinstance].hud.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.6];
    [[YYJ_MBHud shareinstance].hud setMargin:10];
    [[YYJ_MBHud shareinstance].hud setRemoveFromSuperViewOnHide:YES];

    [YYJ_MBHud shareinstance].hud.detailsLabel.text = [msg isKindOfClass:[NSString class]]? msg : @"";
    [YYJ_MBHud shareinstance].hud.contentColor = [UIColor whiteColor];
    [YYJ_MBHud shareinstance].hud.detailsLabel.font = [UIFont systemFontOfSize:14];
    [YYJ_MBHud shareinstance].hud.mode = Co8MBProgressModeLoading;
    if (progress) {
        [YYJ_MBHud shareinstance].hud.progress = progress;
    }
}
@end
